#region Using Statements

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion

namespace _1942
{
    /// <summary>
    /// This game component implements a manager for all Kamikaze planes in the game.
    /// </summary>
    public class MinerManager : DrawableGameComponent
    {
        #region Variables

        // List of active planes
        protected List<Miner> miners;
        // Constant for initialplane count
        private const int STARTMINERCOUNT = 0;
        // Time for a new plane
        private const int ADDMINERTIME = 20000;

        // Audio
        private AudioLibrary audio;

        protected Texture2D planeTexture;
        protected TimeSpan elapsedTime = TimeSpan.Zero;

        private Color[] texturesData;
			 
        //If the planes should reappear
        private bool shouldSpawn = true;

        #endregion

        #region Constructors

        public MinerManager(Game game, ref Texture2D theTexture)
            : base(game)
        {
            planeTexture = theTexture;
            miners = new List<Miner>();

            // Get the audio library
            audio = (AudioLibrary)Game.Services.GetService(typeof(AudioLibrary));

            Rectangle frame = new Rectangle();
            frame.X = 4;
            frame.Y = 400;
            frame.Width = 64;
            frame.Height = 64;

            texturesData = new Color[64 * 64];
            theTexture.GetData<Color>(0, frame, texturesData, 0, frame.Width * frame.Height);

        }

        #endregion

        #region Methods

        /// <summary>
        /// Allows the game component to perform any initialization it needs to 
        /// before starting to run.  This is where it can query for any required
        /// services and load content.
        /// </summary>
        public override void Initialize()
        {
            miners.Clear();

            Start();

            for (int i = 0; i < miners.Count; i++)
            {
                miners[i].Initialize();
            }

            base.Initialize();
        }

        /// <summary>
        /// Start the miner attack
        /// </summary>
        public void Start()
        {
            // Initialize a counter
            elapsedTime = TimeSpan.Zero;

            // Add the planes
            for (int i = 0; i < STARTMINERCOUNT; i++)
            {
                AddNewMiner();
            }
        }

        protected override void UnloadContent()
        {
            this.planeTexture.Dispose();
            base.UnloadContent();
        }

        /// <summary>
        /// Check if is a moment for a new miner
        /// </summary>
        private void CheckforNewMiner(GameTime gameTime)
        {
            // Add a miner-plane each ADDMINERTIME
            elapsedTime += gameTime.ElapsedGameTime;

            if (elapsedTime > TimeSpan.FromMilliseconds(ADDMINERTIME))
            {
                elapsedTime -= TimeSpan.FromMilliseconds(ADDMINERTIME);
                AddNewMiner();
            }
        }

        /// <summary>
        /// Add a new miner in the scene
        /// </summary>
        private Miner AddNewMiner()
        {
            Miner newMiner = new Miner(Game, ref planeTexture);
            newMiner.Initialize();
            miners.Add(newMiner);
            return newMiner;
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (shouldSpawn) CheckforNewMiner(gameTime);

            // Update Planes
            foreach (Miner item in miners)
            {
                if (item.Enabled)
                    item.Update(gameTime);
            }

            miners.RemoveAll(delegate(Miner item)
            {
                if (!item.Enabled)
                    return true;
                return false;
            }
            );
        }

        /// <summary>
        /// Allows the game component draw your content in game screen
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            // Draw the planes
            foreach (Miner item in miners)
            {
                if (item.Visible)
                    item.Draw(gameTime);
            }

            base.Draw(gameTime);
        }

        #endregion

        #region Properties

        public Color[] TextureData
        {
            get { return texturesData; }
        }

        /// <summary>
        /// All kamikazess in the game
        /// </summary>
        public List<Miner> AllMiners
        {
            get { return miners; }
        }	  

       public bool ShouldSpawn
        {
            get { return shouldSpawn; }
            set
            {
                shouldSpawn = value;
                foreach (Miner m in miners)
                {
                    m.ShouldSpawn = false;
                }
            }
        }
        #endregion
    }
}